import coin.proto.coin_order_enums_pb2 as coin_order

_ORDER_SIDE_TO_PROTO = {
    'buy': coin_order.BUY_ORDER,
    'sell': coin_order.SELL_ORDER,
    'bid': coin_order.BUY_ORDER,
    'ask': coin_order.SELL_ORDER,
    'buy_open': coin_order.BUY_OPEN_ORDER,
    'sell_open': coin_order.SELL_OPEN_ORDER,
    'buy_close': coin_order.BUY_CLOSE_ORDER,
    'sell_close': coin_order.SELL_CLOSE_ORDER,
}


def is_buy_side_str(side):
  side = side.lower()
  if side in ('buy', 'bid', 'buy_open', 'buy_close'):
    return True
  elif side in ('sell', 'ask', 'sell_open', 'sell_close'):
    return False
  else:
    raise ValueError('Invalid side %s' % side)


def get_proto_order_side_str(side):
  side = side.lower()
  if side not in _ORDER_SIDE_TO_PROTO:
    raise ValueError('Invalid order side %s' % side)
  return _ORDER_SIDE_TO_PROTO[side]


def get_str_proto_order_side(side):
  for key, value in _ORDER_SIDE_TO_PROTO.items():
    if side == value:
      return key
  raise ValueError('Invalid order_side %s' % side)


def check_order_side(side):
  get_proto_order_side_str(side)


def convert_two_order_side(side):
  side = side.lower()
  assert side in ['ask', 'bid', 'sell', 'buy']
  if side == 'ask':
    side = 'sell'
  elif side == 'bid':
    side = 'buy'
  return side
